module Aio
	# This module adds most of the Arduino Reference functionality.
	#
	# Check the {http://www.arduino.cc/en/reference/homePage Arduino Reference}
	# for more information about them.
	
	module Helpers
		def lowByte(num)
			num & 0xff
		end
		
		def highByte(num)
			num & 0xff00
		end
		
		def bit(n)
			2**n
		end
		
		def bitClear(num, pos)
			num & ~(0x01 << pos)
		end
		
		def bitSet(num, pos)
			num | (0x01 << pos)
		end
		
		def bitRead(num, pos)
			(num & (0x01 << pos)) >> pos
		end
		
		def bitWrite(num, pos, value)
			value == 0 ? bitClear(num, pos) : bitSet(num, pos)
		end
		
		def min(x, y)
			x < y ? x : y
		end
		
		def max(x, y)
			x < y ? y : x
		end
		
		def abs(num)
			num > 0 ? num : -num
		end
		
		def pow(base, exponent)
			base ** exponent
		end
		
		def sqrt(num)
			Math.sqrt(num)
		end
		
		def sin(rad)
			Math.sin(rad)
		end
		
		def cos(rad)
			Math.cos(rad)
		end
		
		def tan(rad)
			Math.tan(rad)
		end
		
		def map(value, min0, max0, min1, max1)
			((value - min0) * (max1 - min1) / (max0 - min0)) + min0
		end
		
		def constrain(value, a, b)
			return a if value < a
			return b if value > b
			value
		end
		
		def random(a, b = nil)
			(a and b) ? rand(a..b) : rand(a)
		end
		
		def randomSeed(seed)
			srand(seed)
		end
	end
end
